﻿using System;
using System.Configuration.Provider;
using System.Web.Configuration;

namespace DBFile.Core.Configuration
{
	/// <summary>
	/// Wrapper around the providers listed in the config file
	/// </summary>
	public sealed class DBFileProviderCollection : ProviderCollection
	{
		private string defaultProviderName;

		/// <summary>
		/// Initializes a new instance of the <see cref="DBFileProviderCollection"/> class.
		/// </summary>
		public DBFileProviderCollection()
		{ }

		/// <summary>
		/// Adds a provider to the collection.
		/// </summary>
		/// <param name="provider">The provider to be added.</param>
		/// <exception cref="T:System.NotSupportedException">The collection is read-only.</exception>
		/// <exception cref="T:System.ArgumentNullException">
		/// 	<paramref name="provider"/> is null.</exception>
		/// <exception cref="T:System.ArgumentException">The <see cref="P:System.Configuration.Provider.ProviderBase.Name"/> of <paramref name="provider"/> is null.- or -The length of the <see cref="P:System.Configuration.Provider.ProviderBase.Name"/> of <paramref name="provider"/> is less than 1.</exception>
		/// <PermissionSet>
		/// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
		/// </PermissionSet>
		public override void Add(ProviderBase provider)
		{
			if (provider == null)
			{
				throw new ArgumentNullException("provider");
			}
			if (!(provider is DBFileProvider))
			{
				throw new ArgumentException("Provider must implement DBFileProvider", "provider");
			}

			base.Add(provider);
		}

		/// <summary>
		/// Copies to the collection to an array
		/// </summary>
		/// <param name="array">The array.</param>
		/// <param name="index">The index.</param>
		public void CopyTo(DBFileProvider[] array, int index)
		{
			base.CopyTo(array, index);
		}

		/// <summary>
		/// Gets the <see cref="DownStream.Core.Configuration.DBFileProvider"/> with the specified name.
		/// </summary>
		/// <value></value>
		public new DBFileProvider this[string name]
		{
			get { return (DBFileProvider)base[name]; }
		}


		/// <summary>
		/// Gets or sets the default name of the provider.
		/// </summary>
		/// <value>The default name of the provider.</value>
		public string DefaultProviderName
		{
			get { return defaultProviderName; }
			set { defaultProviderName = value; }
		}

		/// <summary>
		/// Reads the DB file provider section of the config file
		/// </summary>
		/// <returns></returns>
		public static DBFileProviderCollection ReadDBFileProviderSection()
		{
			DBFileSectionHandler handler = (DBFileSectionHandler)WebConfigurationManager.GetSection("dbfile");
			DBFileProviderCollection providers = new DBFileProviderCollection();
            if (handler != null && handler.Providers.Count > 0)
            {
                ProvidersHelper.InstantiateProviders(handler.Providers, providers, typeof(DBFileProvider));
                providers.DefaultProviderName = handler.DefaultProvider;
            }
			return providers;
		}

	}
}
